package com.th.supcom.pds.drugreturn.client.widget;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.th.supcom.pds.internal.client.model.MedicareCardAllInfoModel;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.widget.MedicareCardField;
import com.th.supcom.pds.recipefee.client.widget.RecipeFeeUtils;

/**
 * 门诊退药主面板.
 * 
 * @author yzhang
 */

public class DrugReturnMainPanel extends ContentPanel
{
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    /** 门诊退药Action */
    private DrugReturnAction m_drugReturnAction;
    /** 门诊退药文本域集合. */
    private DrugReturnAndRefundFields m_drugReturnAndRefundFields;

    /** 门诊退药处方信息面板. */
    private DrugReturnAndRefundPrescPanel m_drugReturnAndRefundPrescPanel;

    /** 门诊退药按钮集合. */
    private DrugReturnButtons m_drugReturnButtons;

    /** 门诊退药处方明细信息面板. */
    private DrugReturnPrescDetailPanel m_drugReturnPrescDetailPanel;

    /** 门诊退药拒绝框 */
    private DrugReturnRefuseWindow m_rugReturnRefuseWindow;
    /** 医保卡刷卡. */
    private MedicareCardField m_medicareCardField;

    /**
     * 添加刷卡组件
     */
    private void addMedicareCard ()
    {
        m_medicareCardField = new MedicareCardField (this)
        {

            @Override
            public void handleEventAfter ()
            {
                PatientInfoModel model = RecipeFeeUtils.initPatientInfo (m_medicareCardField);
                m_drugReturnAction.initPatiInfo (model);
                m_drugReturnAction.queryByPid (model.getPatiId (), null);
                return;
            }
        };
    }

    public DrugReturnMainPanel ()
    {
        initUI ();
        initAction ();
        initListeners ();
    }

    /**
     * 初始化事件
     */
    private void initAction ()
    {
        m_drugReturnAction = new DrugReturnAction ();
        m_drugReturnAction.setDrugReturnMainPanel (this);
        m_drugReturnAction.setDrugReturnFields (m_drugReturnAndRefundFields);
        m_drugReturnAction.setDrugReturnPrescDetailPanel (m_drugReturnPrescDetailPanel);
        m_drugReturnAction.setDrugReturnPrescPanel (m_drugReturnAndRefundPrescPanel);
        m_drugReturnAction.setDrugReturnRefuseWindow (m_rugReturnRefuseWindow);
    }

    /**
     * 初始化事件监听
     */
    private void initListeners ()
    {
        /** 门诊退药Listener */
        final DrugReturnListener drugReturnListener = new DrugReturnListener ();
        drugReturnListener.setDrugReturnAction (m_drugReturnAction);
        drugReturnListener.setDrugReturnButtons (m_drugReturnButtons);
        drugReturnListener.setDrugReturnFields (m_drugReturnAndRefundFields);
        drugReturnListener.setDrugReturnRefuseWindow (m_rugReturnRefuseWindow);
        drugReturnListener.setDrugReturnAndRefundPrescPanel (m_drugReturnAndRefundPrescPanel);
        drugReturnListener.setKeyNavComponent (this);
        drugReturnListener.initListeners ();

    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        setHeaderVisible (false);
        setLayout (new BorderLayout ());
        // 初始化对象

        m_drugReturnButtons = new DrugReturnButtons ();
        m_drugReturnAndRefundFields = new DrugReturnAndRefundFields ();
        addMedicareCard ();
        m_medicareCardField.setFieldLabel ("刷卡");
        m_drugReturnAndRefundFields.setMedicareCardField (m_medicareCardField);

        DrugReturnInfoPanel drugReturnInfoPanel = new DrugReturnInfoPanel (m_drugReturnAndRefundFields,
                                                                           m_drugReturnButtons, m_medicareCardField);

        final DrugReturnPatientInfoPanel drugReturnPatientInfoPanel = new DrugReturnPatientInfoPanel (
                                                                                                      m_drugReturnAndRefundFields);
        m_drugReturnPrescDetailPanel = new DrugReturnPrescDetailPanel ();
        m_drugReturnAndRefundPrescPanel = new DrugReturnAndRefundPrescPanel ();
        m_rugReturnRefuseWindow = new DrugReturnRefuseWindow ();

        final LayoutContainer northPanelContainer = new LayoutContainer ();
        northPanelContainer.setLayout (new RowLayout (Orientation.VERTICAL));
        northPanelContainer.add (drugReturnInfoPanel, new RowData (1, 0.5, new Margins (0)));
        northPanelContainer.add (drugReturnPatientInfoPanel, new RowData (1, 0.5, new Margins (0)));
        // 设置布局和大小
        final BorderLayoutData northData = new BorderLayoutData (LayoutRegion.NORTH, 180);
        northData.setMargins (new Margins (4));
        final BorderLayoutData westData = new BorderLayoutData (LayoutRegion.WEST, 272);
        westData.setMargins (new Margins (0, 4, 4, 4));
        final BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (0, 4, 4, 0));

        add (northPanelContainer, northData);
        add (m_drugReturnAndRefundPrescPanel, westData);
        add (m_drugReturnPrescDetailPanel, centerData);
        // 初始化下部按钮
        final ContentPanel bottomContainer = new ContentPanel ();
        bottomContainer.setHeaderVisible (false);
        bottomContainer.setButtonAlign (HorizontalAlignment.CENTER);
        bottomContainer.addButton (m_drugReturnButtons.getDrugReturnBtn ());
        bottomContainer.addButton (m_drugReturnButtons.getRefuseBtn ());
        bottomContainer.addButton (m_drugReturnButtons.getClearBtn ());
        setBottomComponent (bottomContainer);
        m_drugReturnAndRefundFields.getMedicareCardField ().focus ();
    }
}